﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Runtime.Serialization;

namespace IssueServiceLibrary
{
    [Serializable]
    public class IssueList
    {
        public const string IssueFileName = "issues.xml";
        
        List<Issue> isses;

        public List<Issue> Issues
        {
           get { return isses;}
           set {isses = value;}
        }

        /// <summary>
        /// default constructor for serialization
        /// </summary>
        public IssueList()
        {
            isses = new List<Issue>();
        }
  
        public static IssueList ReadIssuesSerializable()
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(IssueList));
            
            if(File.Exists(IssueFileName))
            {
                using (FileStream fs = File.OpenRead(IssueFileName))
                {
                    IssueList issList = (IssueList)dcs.ReadObject(fs);
                    return issList;
                }
            }
            else
                return new IssueList();
        }

        public static void WriterIssuesSerializable(IssueList issList)
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(IssueList));
            using (FileStream fs = new FileStream(IssueFileName, FileMode.Create))
            {
                dcs.WriteObject(fs, issList);
            }
        }
    }
}
